﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Generator.Tools
{
    public partial class StringFormat : ToolsBase
    {
        public StringFormat()
        {
            InitializeComponent();
        }

        private void FormatString()
        {
            rtResult.Text = "";
            var format=GetFormatText();
            string optionEnter = string.Empty;
            if (chkEnter.Checked)
                optionEnter = "\"+  Environment.NewLine + " + Environment.NewLine + "\"";

            string[] lines = rtSource.Text.Split( new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);
            foreach (var txt in lines)
            {
                if (txt.Trim().Length == 0)
                    continue;
                                
                string[] strs = txt.Split(new char[] { '\t' });
                string[] strToFormat = new string[Math.Max(format.Item2, strs.Length)];

                for (int i = 0; i < strs.Length; i++)
                {
                    string[] ml = strs[i].Split(Environment.NewLine.ToCharArray(), StringSplitOptions.None);
                    if (ml.Length > 1)
                    {

                        strToFormat[i] = ml.Aggregate((workingSentence, next) =>
                                                    workingSentence + optionEnter + next);
                    }
                    else
                    {
                        strToFormat[i] = i == 0 ? strs[i] : "\t" + strs[i];
                    }
                }

                try
                {
                    string tmpStr = string.Format(format.Item1, strToFormat);
                    rtResult.AppendText(tmpStr);
                }
                catch (Exception ex)
                {
                    rtResult.AppendText(ex.Message);
                    rtResult.AppendText(Environment.NewLine);
                    rtResult.AppendText("--------------------------------------------------");
                }
            }

        }

        private Tuple<string,int> GetFormatText()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(rtFormat.Text.Replace("{", "{{").Replace("}", "}}"));

            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\{\d+\}");
            var matches= reg.Matches(sb.ToString());
            for (int i = matches.Count - 1; i >= 0; i--)
            {
                var match = matches[i];
                var newValue= match.Value.Substring(1, match.Value.Length - 2);
                sb.Remove(match.Index, match.Length);
                sb.Insert(match.Index, newValue);
            }
            return new Tuple<string, int>(sb.ToString(), matches.Count);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FormatString();
        }
    }
}
